package cn.fivestars.springcloud.order.controller;

import cn.fivestars.springcloud.commons.entities.CommonResult;
import cn.fivestars.springcloud.commons.entities.Payment;
import cn.fivestars.springcloud.order.service.PaymentFeignService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author liangchenzhou1024@163.com
 * @date 2021/2/5
 */
@RestController
@RequestMapping("/order")
@Slf4j
@DefaultProperties(defaultFallback = "globalHandleTimeOut")
public class OrderHystrixController {

    @Resource
    private PaymentFeignService paymentFeignService;

    @GetMapping("/{id}")
    public <T> CommonResult<T> getById(@PathVariable("id") Long id) {
        return paymentFeignService.getById(id);
    }

    @GetMapping("/timeout/{id}")
    //@HystrixCommand(fallbackMethod = "handleTimeOutOfSelectByIdTimeout",
    //        commandProperties = {
    //                @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    //        })
    public <T> CommonResult<T> timeout(@PathVariable Long id) {
        // openFeign 默认等待1s
        return paymentFeignService.timeout(id);
    }

    @GetMapping("/timeout_global/{id}")
    //@HystrixCommand
    public <T> CommonResult<T> timeoutGlobal(@PathVariable Long id) {
        // openFeign 默认等待1s
        return paymentFeignService.timeout(id);
    }

    //public <T> CommonResult<T> handleTimeOutOfSelectByIdTimeout(Long id)  {
    //    log.warn("fallback:handleTimeOutOfSelectByIdTimeout:id={}", id);
    //    return CommonResult.failure(999, "service error");
    //}
    //
    //public <T> CommonResult<T> globalHandleTimeOut()  {
    //    log.warn("fallback:globalHandleTimeOut");
    //    return CommonResult.failure(999, "service error");
    //}

}
